using System.Collections.Immutable;
using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisDate : AnalysisExpression
{
    internal AnalysisDate(Expression syntaxNode, in DateTime dateTime, AnalysisNode? parent) :
        base(AnalysisBoundKinds.Literal, syntaxNode, parent)
    {
        Value = dateTime;
        Bind(ValueTypeSymbol.Date);
    }


    /// <summary>
    ///     日期值
    /// </summary>
    public DateTime Value { get; }


    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return ImmutableArray<AnalysisNode>.Empty;
    }

    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisDate cannot have children.");
    }

    public override AnalysisExpression? WithImplicitlyConversion(ValueTypeSymbol targetType)
    {
        if (ValueTypeSymbol.IsAnyOrNull(targetType) || targetType == ValueTypeSymbol.Date)
        {
            return this;
        }

        // Boolean
        return targetType == ValueTypeSymbol.Boolean
            ? new AnalysisBoolean(NodeConverter.ToBoolean(Syntax, true), Parent)
                .WithContainingWithSymbol(GetContainingWithSymbol())
            :
            // 不主动将日期类型转换为字符串或数字，不支持转换为分类常量
            null;
    }
}


public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个日期常量表达式
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="dateTime">日期值</param>
    /// <param name="parent">父节点</param>
    public static AnalysisDate Date(Expression syntaxNode, in DateTime dateTime, AnalysisNode? parent)
{
    return new AnalysisDate(syntaxNode, dateTime, parent);
}
}